import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
export const Layout = () => import('@/layout/index.vue')

import whiteListRoutes from './modules/whiteList'
import geoBasicRoutes from './modules/geoBasic'
import geoRoutes from './modules/geo'

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
    {
        path: '/redirect',
        component: Layout,
        meta: {
            hidden: true
        },
        children: [
            {
                path: '/redirect/:path(.*)',
                component: () => import('@/views/redirect/index.vue')
            }
        ]
    },
    {
        path: '/login',
        component: () => import('@/views/login/index.vue'),
        meta: {
            hidden: true
        }
    },
    {
        path: '/403',
        component: Layout,
        meta: {
            hidden: true
        },
        children: [
            {
                path: '',
                component: () => import('@/views/error/403.vue'),
            }
        ]
    },
    {
        path: '/404',
        component: Layout,
        meta: {
            hidden: true
        },
        children: [
            {
                path: '',
                component: () => import('@/views/error/404.vue'),
            }
        ]
    },
    {
        path: '/500',
        component: Layout,
        meta: {
            hidden: true
        },
        children: [
            {
                path: '',
                component: () => import('@/views/error/500.vue'),
            }
        ]
    },
    {
        path: '/demo',
        component: () => import('@/views/error/demo.vue'),
        meta: {
            hidden: true
        },
        children: [

        ]
    },

    ...whiteListRoutes,

]

// 创建路由
const router = createRouter({
    history: createWebHashHistory(),
    routes: constantRoutes as RouteRecordRaw[],
    scrollBehavior: () => ({ top: 0, left: 0 })
});

// 重置路由
export function resetRouter() {
    router.replace({
        path: '/login'
    })
}

// 1. 在路由守卫中持久化路由状态
router.beforeEach((to, from, next) => {
    sessionStorage.setItem('X-currentRoute', JSON.stringify({
        path: to.path,
        query: to.query,
        params: to.params
    }))
    next()
})

export default router